﻿#include "FITKSEASubSysModelProp.h"
#include "FITKSEASubSysBeam.h"
#include "FITKSEASubSysAbsShell.h"
#include "FITKSEASubSysCavity.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsSEAModel.h"
#include "FITKAcousticsDiscNodeList.h"
#include "FITKAbstractSEAFace.h"
#include "FITKSEASubSysShellCurved.h"
#include "FITKAcousticsTriangles.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementTri.h"
#include "FITK_Component/FITK2DMeshTools/FITKAbstractRecogClosedSpace.h"
#include "FITK_Kernel/FITKCore/FITKVec3DAlg.h"
#include "FITK_Kernel/FITKCore/FITKTriangleAlg.h"
#include <cmath>

namespace Acoustics
{
    FITKSEASubSysModelProp::FITKSEASubSysModelProp(FITKAbstractSEAFace* faceData)
        :_faceData(faceData)
    {
    }

    QList<QPair<int, int>> FITKSEASubSysModelProp::getBoundary()
    {
        return _boundaryNodes;
    }

    bool FITKSEASubSysModelProp::updateModelProp()
    {
        this->calculateBoundaryEdge();
        return true;
    }

    void FITKSEASubSysModelProp::calculateBoundaryEdge()
    {
        if (!_faceData)return;
        FITKAcousticsTriangles* mesh = _faceData->getTriangleMesh();
        if (!mesh)return;
    //获取节点和网格数据
        QList<int> nodeSubSysList = mesh->getAllNodeIDs();
        if (nodeSubSysList.isEmpty() || !mesh)return;
    //获取节点管理器 
        FITKAcousticsDiscModel *discModel = FITKAPP->getGlobalData()->getMeshData<FITKAcousticsDiscModel>();
        if (!discModel) return;
        FITKAcousticsDiscNodeList *discNodeList = discModel->getNodes();
        if (!discNodeList) return;
    //存储点和单元
        QList<Core::FITKNode*> nodeList;
        QList<Interface::FITKElementTri3*> eleList;
    //获取节点数据
        for (int nID : nodeSubSysList)
        {
            Core::FITKNode *node = discNodeList->getNodeByID(nID);
            if (!node)continue;
            nodeList.append(node);
        }
    //获取单元数据
        int eleNum = mesh->getElementCount();
        for (int i = 0; i < eleNum; ++i)
        {
            Interface::FITKElementTri3* ele = dynamic_cast<Interface::FITKElementTri3*>(mesh->getElementAt(i));
            if (!ele) continue;
            eleList.append(ele);
        }
    //获取边界数据
        Interface::FITKAbstractRecogClosedSpace * space = Interface::FITKAbstractRecogClosedSpace::createFITKRecogClosedSpace();
        if (!space->initMesh(nodeList, eleList)) return;
        QList<QPair<int, int>> bound = space->getBoundary();
        for (QList<QPair<int, int>>::iterator iter = bound.begin(); iter < bound.end(); ++iter)
        {
            int nodeIndex1 = iter->first, nodeIndex2 = iter->second;
            if (nodeList.size() <= nodeIndex1 || nodeList.size() <= nodeIndex2)continue;
            Core::FITKNode *node1 = nodeList.at(nodeIndex1), *node2 = nodeList.at(nodeIndex2);
            if (!node1||!node2)continue;
            int node1ID = node1->getNodeID();
            int node2ID = node2->getNodeID();
            _boundaryNodes.append(QPair<int, int>(node1ID, node2ID));
        }
        delete space;
    }
}


